package com.qf.sl.youshare.fragments;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import com.qf.sl.youshare.R;
import com.qf.sl.youshare.activity.ShareContentActivity;
import com.qf.sl.youshare.adapters.SheJiaoShareAdapter;
import com.qf.sl.youshare.model.Article;
import com.qf.sl.youshare.tasks.ShareArticleTask;
import com.qf.sl.youshare.tasks.TaskProcessor;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.LinkedList;

/**
 * Created by IDEA
 * User : SL
 * on  2015/2/17.
 * 22:19
 */
public class SheJiaoShareFragment extends Fragment implements AdapterView.OnItemClickListener, TaskProcessor {

    private boolean hasFirstLoad = false;
    private LinkedList<Article> articles;
    private SheJiaoShareAdapter adapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View ret = inflater.inflate(R.layout.fragment_shejiao_share, container, false);

        ListView listView = (ListView) ret.findViewById(R.id.shejiao_share_list);

        if (listView != null) {

            articles = new LinkedList<Article>();
            adapter = new SheJiaoShareAdapter(getActivity(), articles);

            listView.setAdapter(adapter);

            listView.setOnItemClickListener(this);

        }
        if (!hasFirstLoad){
            ShareArticleTask task = new ShareArticleTask(this);
            task.execute();//可传递参数 size, 默认为 10
        }

        return ret;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Article article = articles.get(position);
        String content = article.getContent();
        Intent intent = new Intent(getActivity(), ShareContentActivity.class);
        intent.putExtra("content",content);
        startActivity(intent);
    }

    @Override
    public void processResult(JSONObject jsonObject) {
        if (jsonObject != null) {

            try {
                int code = jsonObject.getInt("code");
                if (code == 1){
                    JSONObject msg = jsonObject.getJSONObject("msg");
                    JSONArray data = msg.getJSONArray("data");

                    LinkedList<Article> list = new LinkedList<Article>();

                    for (int i = 0; i < data.length(); i++) {
                        JSONObject dataObject = data.getJSONObject(i);
                        Article article = new Article();
                        article.parseJSON(dataObject);
//                        articles.add(article);
//                        System.out.println("articles.size() = " + articles.size());

                        list.add(article);

                    }
                    articles.addAll(list);
                    adapter.notifyDataSetChanged();
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

        }
        hasFirstLoad = true;
    }
}